package com.yonyou.gmmc.service.pointactivity.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.MultipartConfigElement;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.marketing.common.constants.ResultMsgConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.yonyou.gmmc.common.annotation.ParamValid;
import org.marketing.common.Response;
import com.yonyou.gmmc.service.pointactivity.constant.PointActivityConstant;
import com.yonyou.gmmc.service.pointactivity.service.GoToLhasaActivityService;

import io.swagger.annotations.Api;

/*
 * 积分活动
 */
@RestController
@RequestMapping(value = PointActivityConstant.BASE_PATH + "/" + PointActivityConstant.VERSION)
@Api("去拉萨活动")
public class GoToLhasaActivityController {

	private Logger logger = Logger.getLogger(GoToLhasaActivityController.class);

	@Autowired
	private GoToLhasaActivityService goToLhasaActivityService;
	
	 @Bean
	 MultipartConfigElement multipartConfigElement() {
	    MultipartConfigFactory factory = new MultipartConfigFactory();
	    factory.setMaxFileSize("10240KB");
	    factory.setMaxRequestSize("10240KB");
	    factory.setLocation("/home/lyh_admin/appfile/fs01");
	    return factory.createMultipartConfig();
	}

	/**
	 * 进入页面初始化页面时,调用的方法
	 * 
	 * @param req
	 * @return Map
	 */
	@RequestMapping(value = "/goToLhasaInit", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public @ResponseBody Map<String, Object> goToLhasaInit(HttpServletRequest req) {

		String openId = req.getHeader(PointActivityConstant.HEADER_OPENID);

		logger.info("去拉萨活动初始化方法获取到的的openId为:" + openId);

		// openId = null == openId ? "oXrcfuC1R4wosEPeJOHR7BPqaAOo" : openId;

		// logger.info("去拉萨活动初始化方法获取到的的openId为==========:" + openId);

		try {
			goToLhasaActivityService.goToLhasaVisitRecord(openId);
		} catch (Exception e) {
			logger.info("去拉萨活动记录访问方法获取到的的openId为:" + openId + ",记录失败.");
		}

		Map<String, Object> resultMap = new HashMap<String, Object>();

		try {
			resultMap = goToLhasaActivityService.goToLhasaInit(openId);
			resultMap.put("activationEndDate", goToLhasaActivityService.getActivationEndDate());
		} catch (Exception e) {
			logger.error(new Exception(e));
			resultMap.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
			resultMap.put(ResultMsgConstants.ERR_MSG, "去拉萨活动页面初始化异常");
			resultMap.put(ResultMsgConstants.QUERY_DATA, "");
		}
		return resultMap;
	}

	/**
	 * 抽奖初始化
	 * 
	 * @return Map
	 */
	@RequestMapping(value = "/luckDrawInit", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public @ResponseBody Map<String, Object> luckDrawInit() {

		Map<String, Object> resultMap = new HashMap<String, Object>();

		try {
			resultMap = goToLhasaActivityService.getLuckDrawDate();
		} catch (Exception e) {
			logger.error(new Exception(e));
			resultMap.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
			resultMap.put(ResultMsgConstants.ERR_MSG, "去拉萨活动抽奖初始化异常");
			resultMap.put(ResultMsgConstants.QUERY_DATA, "");
		}
		return resultMap;
	}

	/**
	 * 抽奖
	 * 
	 * @return Map
	 */
	@RequestMapping(value = "/luckDraw", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public synchronized @ResponseBody Map<String, Object> luckDraw(HttpServletRequest req) {

		String openId = req.getHeader(PointActivityConstant.HEADER_OPENID);

		logger.info("去拉萨活动抽奖方法获取到的的openId为:" + openId);

		// openId = null == openId ? "oXrcfuC1R4wosEPeJOHR7BPqaAOo" : openId;

		// logger.info("去拉萨活动抽奖方法获取到的的openId为==========:" + openId);

		Map<String, Object> resultMap = new HashMap<String, Object>();

		try {
			resultMap = goToLhasaActivityService.luckDraw(openId);
		} catch (Exception e) {
			logger.error(new Exception(e));
			resultMap.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
			resultMap.put(ResultMsgConstants.ERR_MSG, e.getMessage());
			resultMap.put(ResultMsgConstants.QUERY_DATA, "");
		}
		return resultMap;
	}

	/**
	 * 抽奖初始化
	 * 
	 * @return Map
	 */
	@RequestMapping(value = "/getUserLottery", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public @ResponseBody Map<String, Object> lotteryInit(HttpServletRequest req) {

		String openId = req.getHeader(PointActivityConstant.HEADER_OPENID);

		logger.info("去拉萨活动抽奖方法获取到的的openId为:" + openId);

		// openId = null == openId ? "oXrcfuC1R4wosEPeJOHR7BPqaAOo" : openId;

		// logger.info("去拉萨活动抽奖方法获取到的的openId为==========:" + openId);

		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = goToLhasaActivityService.getUserLottery(openId);
		} catch (Exception e) {
			logger.error(new Exception(e));
			resultMap.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
			resultMap.put(ResultMsgConstants.ERR_MSG, e.getMessage());
			resultMap.put(ResultMsgConstants.QUERY_DATA, "");
		}
		return resultMap;
	}

	/**
	 * 抽奖初始化
	 * 
	 * @return Map
	 */
	@RequestMapping(value = "/lotteryInit", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public @ResponseBody Map<String, Object> lotteryInit(Integer oneDayNum, String startDate, String endDate, String activityStartDate, String activityEndDate, boolean isAgain, @RequestParam("isAutomatic") boolean isAutomatic) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			goToLhasaActivityService.lotteryInit(startDate, endDate, activityStartDate, activityEndDate, oneDayNum, isAgain, isAutomatic);
		} catch (Exception e) {
			logger.error(new Exception(e));
			resultMap.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
			resultMap.put(ResultMsgConstants.ERR_MSG, e.getMessage());
			resultMap.put(ResultMsgConstants.QUERY_DATA, "");
		}
		return resultMap;
	}

	/**
	 * 上传相片
	 * 
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getOpenId", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> getOpenId(HttpServletRequest req) throws Exception {

		String openId = req.getHeader(PointActivityConstant.HEADER_OPENID);

		logger.info("去拉萨活动抽奖方法获取到的的openId为:" + openId);

		Map<String, Object> resultMap = new HashMap<String, Object>();

		try {
			resultMap.put("openId", openId);
		} catch (Exception e) {
			logger.error(new Exception(e));
			resultMap.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
			resultMap.put(ResultMsgConstants.ERR_MSG, e.getMessage());
			resultMap.put(ResultMsgConstants.QUERY_DATA, "");
		}
		return resultMap;

	}

	/**
	 * 上传相片
	 * 
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@ParamValid
	@RequestMapping(value = "/uploadPhoto", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
	public String uploadPhoto(HttpServletRequest request) throws Exception {
		try {
			String openId = request.getHeader(PointActivityConstant.HEADER_OPENID);
			logger.info("查询openId开始" + openId);
			// String openId = "oXrcfuCpMJXvTH9L38ajQmEuaoxw";
			goToLhasaActivityService.uploadPhoto(openId, request);
			// Cookie
			// response.addCookie();
			return "1";
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return "2";
		}
	}

	/**
	 * 查询已上传的照片
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/viewPhoto", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> viewPhoto(HttpServletRequest req) {

		String openId = req.getHeader(PointActivityConstant.HEADER_OPENID);

		logger.info("去拉萨活动抽奖方法获取到的的openId为:" + openId);

		// openId = null == openId ? req.getParameter("openId") : openId;

		// logger.info("去拉萨活动抽奖方法获取到的的openId为2:" + openId);

		// logger.info("去拉萨活动抽奖方法获取到的的openId为==========:" + openId);

		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = goToLhasaActivityService.viewPhoto(openId);
		} catch (Exception e) {
			logger.error(new Exception(e));
			resultMap.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
			resultMap.put(ResultMsgConstants.ERR_MSG, e.getMessage());
			resultMap.put(ResultMsgConstants.QUERY_DATA, "");
		}
		return resultMap;
	}

	/**
	 * 定时将跨阶段奖券的激活时间调整往下.
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/lotteryTask", method = RequestMethod.GET)
	public @ResponseBody void lotteryTask() {
		try {
			goToLhasaActivityService.lotteryTask();
		} catch (Exception e) {
			logger.error(new Exception(e));
		}
	}

}
